package com.woniu.service.impl;

import com.woniu.dto.DrugsDto;
import com.woniu.exception.DIYException;
import com.woniu.model.HisMedicine;
import com.woniu.mapper.HisMedicineMapper;
import com.woniu.param.DeleteDrugsByListIdParam;
import com.woniu.param.DrugsParam;
import com.woniu.service.IHisMedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-12
 */
@Service
public class HisMedicineServiceImpl extends ServiceImpl<HisMedicineMapper, HisMedicine> implements IHisMedicineService {

    @Override
    public DrugsDto getDrugsByid(Integer DrugsId) {
        HisMedicine hisMedicine = getById(DrugsId);
        if (hisMedicine==null){
            new DIYException(-9999,"服务器异常");
        }

        DrugsDto drugsDto = CopyBeanUtil.copyBean(hisMedicine, DrugsDto.class);

        return drugsDto;
    }

    @Override
    public DrugsDto AddDrugs(DrugsParam drugsParam) {
        HisMedicine hisMedicine = CopyBeanUtil.copyBean(drugsParam, HisMedicine.class);

        if (!save(hisMedicine)){
            throw  new DIYException(-9999,"服务器异常");
        }
        DrugsDto drugsDto = CopyBeanUtil.copyBean(hisMedicine, DrugsDto.class);

        return  drugsDto  ;
    }

    @Override
    public Boolean DelDrugs(DeleteDrugsByListIdParam deleteDrugsByListIdParam) {
        List<Integer> medicineId = deleteDrugsByListIdParam.getMedicineId();
        boolean flag = removeByIds(medicineId);

        if (!flag){
           throw  new DIYException(-9999,"服务器异常");
       }else {
            for (Integer integer : deleteDrugsByListIdParam.getMedicineId()) {


            }

        }

        return flag ;
    }

    @Override
    public DrugsDto UpdateDrugs(DrugsParam drugsParam) {
        HisMedicine hisMedicine = CopyBeanUtil.copyBean(drugsParam, HisMedicine.class);
        if (!updateById(hisMedicine)) {
           throw  new DIYException(-9999,"服务器异常");
        }
        DrugsDto drugsDto = CopyBeanUtil.copyBean(hisMedicine, DrugsDto.class);

        return  drugsDto;
    }
}
